import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { INestApplication } from '@nestjs/common';

/**
 * 配置并设置Swagger文档
 * @param app Nest应用实例
 */
export function setupSwagger(app: INestApplication): void {
  const config = new DocumentBuilder()
    .setTitle('Nest AI Tools API')
    .setDescription(
      'AI工具服务API文档，包含Anthropic和DeepSeek等多种AI模型的接口',
    )
    .setVersion('1.0')
    .addTag('ai', 'AI相关接口')
    .addTag('anthropic', 'Anthropic模型相关接口')
    .addTag('deepseek', 'DeepSeek模型相关接口')
    .addBearerAuth({
      type: 'http',
      scheme: 'bearer',
      bearerFormat: 'JWT',
      name: 'JWT',
      description: '输入你的JWT token',
      in: 'header',
    })
    .addApiKey({
      type: 'apiKey',
      name: 'x-api-key',
      in: 'header',
      description: '输入你的API key',
    })
    .build();

  const document = SwaggerModule.createDocument(app, config, {
    deepScanRoutes: true,
    extraModels: [],
  });

  // 设置Swagger UI
  SwaggerModule.setup('api-docs', app, document, {
    swaggerOptions: {
      persistAuthorization: true,
      docExpansion: 'list',
      filter: true,
      showExtensions: true,
      tagsSorter: 'alpha',
      operationsSorter: 'alpha',
    },
    customCss: '.swagger-ui .topbar { display: none }',
    customSiteTitle: 'Nest AI Tools API 文档',
  });
}
